package com.example.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
 * @author:我叫程晓非
 * @desc:adb操作工具类
 * @date:2021-11-07
 * @version:1.0
 */
/*
 * adb命令执行工具
 常用adb命令
 adb shell input tap x1 y1 #点击(x1,y1)
 adb shell input swipe x1 y1 x2 y2 100 #100ms（x1,y1）滑动至(x2,y2)
 adb shell wm size #获取分辨率
 adb shell getprop ro.product.name #获取手机型号
 */
public class AdbShellUtil {
    public static Map<String, String> getAllDeviceInfo() {

        Map<String, String> map = null;

        ArrayList<String> list = null;
        Process process;
        BufferedReader reader;
        String line = null;
        String device_type;
        try {
            list = new ArrayList<String>();
            map = new HashMap<String, String>();
            //设置adb.exe存放路径，如果设置了环境变量，直接输入adb即可
            String adb_str = "adb";
            process = executeShell(adb_str + " devices -l");
            if(process != null){
                reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    if (line.length() > 1) {
                        list.add(line);
                    }
                }
                if(!list.contains("* daemon started successfully *")){
                    if (list != null && list.size() > 1) {
                        if(!list.contains("device")){
                            for (int i = 1; i < list.size(); i++) {
                                for (int j = 0; j < list.get(i).split(" ").length; j++) {
                                    //获取手机设备连接状态，目前状态有：device(正常)、offline、unauthorized
                                    device_type = list.get(i).split(" ")[j];
                                    //判断当前设备状态是否正常
                                    if(device_type.equals("device")){
                                        //获取设备序列号
                                        String device_sn = list.get(i).split(" ")[0];
//                                        System.out.println("当前设备名称为:"+ device_sn);
                                        String command_str = adb_str + " -s " + device_sn + " shell getprop ro.product.name";
                                        process = executeShell(command_str);
                                        if(process != null){
                                            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                                            line = null;
                                            while ((line = reader.readLine()) != null) {
                                                map.put(device_sn, line);
                                            }
                                        }
                                    }
                                }
                            }
                        }else{
                            System.out.println("当前设备列表中没有已连接设备，请检查！");
                        }
                    }else{
                        System.out.println("当前设备列表没有连接的设备，请检查！");
                    }
                }else{
                    System.out.println("未知错误。。。");
                }
            }else{
                System.out.println("当前执行adb命令异常，请检查adb环境！");
            }
        } catch (IOException e) {
            System.err.println("IOException" + e.getMessage());
            return null;
        }
        return map;
    }

    public static Process executeShell(String cmd) {
        Process proc = null;
        Runtime runtime = Runtime.getRuntime();
        try {
            proc = runtime.exec(cmd);
        } catch (Exception e) {
            System.out.println("" + cmd );
            return null;
        }
        return proc;
    }
    public static synchronized String executeShell2(String cmd) {
        StringBuffer sb = new StringBuffer("");
        Process proc = null;
        Runtime runtime = Runtime.getRuntime();
        BufferedReader reader;
        try {
            proc = runtime.exec(cmd);
        } catch (Exception e) {
            System.out.println(cmd );
            return null;
        }
        if(proc != null){
            try{
                reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                    sb.append("\n");
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
//    public static void getDeviceScreenshot(String screenshotName){
//        executeShell2("adb shell screencap -p /sdcard/screen.png");
//        executeShell2("adb pull /sdcard/screen.png D://book//"+screenshotName+".png");
//    }


}
